<?php
namespace Kxdrmobile\Model;
use Think\Model;
class FriendshipsModel extends Model{
	public $guest_mixednick; //朋友的昵称
	public $home_mixednick; //自己的昵称
	public $guest_id; //朋友的ID
	public $home_id; //自己的ID
	public $activity_id; //平台的折扣ID
	public $promo_id; //本地折扣ID
	public $promo_cat_id; //本地活动ID
	public $invite_order; //邀请顺序
	public $sid;
	public function addFriendShip(){
		$data["home_mixednick"] = $this->home_mixednick;
		$data["guest_mixednick"] = $this->guest_mixednick;
		$data["home_id"] = $this->home_id;
		$data["guest_id"] = $this->guest_id;
		$data["invite_time"] = date("Y-m-d H:i:s");
		$data["sid"] = $this->sid;
		$data["invite_order"] = $this->invite_order;
		return $this->add($data);
	}
	public function getFriendShip(){
		$data["sid"] = $this->sid;
		$data["home_id"] = $this->home_id;
		$data["guest_id"] = $this->guest_id;
		return $this->where($data)->getField("`id`,`invite_time`");
	}
	public function invite(){
			$USR = D("User");
			if(!$this->home_id && $this->home_mixednick){
				$USR->sid = $this->sid;
				$USR->mixednick = $this->home_mixednick;
				$this->home_id = $USR->getUser();
			}
			$where["guest_id"]=$this->guest_id;
			$where["home_id"]=$this->home_id;
			$where["sid"]=$this->sid;
			$ship = $this->cache(!APP_DEBUG)->where($where)->getField("`id`,`invite_time`");
					
			if(!$ship){
				$USR->sid=$this->sid;
				$USR->uid = $this->home_id;
				$USR->friendsUp();//朋友数量增加写入到数据库
				
				//准备存入朋友关系。朋友链的主客关系刚好相反
				$USR->myfields="friends";
				$this->invite_order = $USR->getUserField();
				$this->addFriendShip(); //朋友关系增加
			}
			return $this->invite_order;
	}
	public function get_code_img(){
		$USR = M("User");
		$udata["sid"] = $this->sid;
		$udata["id"] = $this->home_id;
		$friend = $USR->where($udata)->getField("qrcode_url,friend_link,short_url");

		if($friend["qrcode_url"]){
			$resp = new \stdClass();
			$resp->modules->qrcode_d_o->qrcode_url = $friend["qrcode_url"];
			$resp->modules->qrcode_d_o->url = $friend["friend_link"];
			$resp->modules->qrcode_d_o->short_url = $friend["short_url"];
			$resp->suc=true;
		}else{
			Vendor("taobaosdk.top.TopClient");
			Vendor("taobaosdk.lotusphp_runtime.Logger.Logger");
			Vendor("taobaosdk.top.RequestCheckUtil");
			Vendor("taobaosdk.top.request.MaQrcodeCommonCreateRequest");
			Vendor("taobaosdk.top.request.WirelessXcodeCreateRequest");
			$c = new \TopClient;
			$c->appkey = C("SM_KEY");//用后台的key
			$c->secretKey = C("SM_SECRET"); 
			$req = new \MaQrcodeCommonCreateRequest;
			$req->setType("url");
			$req->setContent(C("URL")."/?friendnick=".urlencode($this->home_mixednick)."&sid=".$this->sid."&cid=".$this->catid);
			$req->setName("淘小店的无线店铺首页");
			$req->setStyle("EF4F2B");
			$req->setSize("300");
			$req->setNeedEps("false");
			$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");

			$SHOP = D("Shop");
			$sessionKey = $SHOP->cache(!APP_DEBUG)->where("`sid`=".$this->sid)->getField("sessionkey");

			$resp = $c->execute($req, $sessionKey);

			if($resp->suc && $this->home_id){
				$sa["id"]=$this->home_id;
				// $sa["sid"]=$this->sid;
				$sa["qrcode_url"] = (string)$resp->modules->qrcode_d_o->qrcode_url;
				$sa["short_url"] = (string)$resp->modules->qrcode_d_o->short_url;
				$sa["eps_url"] = (string)$resp->modules->qrcode_d_o->eps_url;
				$sa["channel_id"] = (string)$resp->modules->qrcode_d_o->channel_id;
				$sa["channel_name"] = (string)$resp->modules->qrcode_d_o->channel_name;
				$re = $USR->save($sa);
			}
		}
		$resp = json_decode(json_encode($resp),true);
		return ($resp);
	}
	
}